<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增文件信息')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-uploadfile-add">
            <div class="form-group">
			    <label class="col-sm-3 control-label">上传文件：</label>
			    <div class="col-sm-8">
			        <input id="fileinput" name="filePath"type="file" multiple>
			        <input name="fileId" th:value="${fileId}" type="hidden">
					<input name="modelName" th:value="${model}" type="hidden">
			    </div>
			</div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "company/dynamic"
        $("#form-uploadfile-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
        	uploadFile();
        }
      //上传方法
        function uploadFile() {
            var formData = new FormData();
            if ($('#fileinput')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            for (var i=0;i<$('#fileinput')[0].files.length;i++) {
                formData.append('fileId', $("input[name='fileId']").val());
                formData.append('model', $("input[name='modelName']").val());
                formData.append('file', $('#fileinput')[0].files[i]);
                $.ajax({
                    url: prefix + "/upload",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
                formData = new FormData();
            }

            $.modal.alertWarning("上传成功");
        }
    </script>
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
    $(document).ready(function () {
        $("#fileinput").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': '#',
            overwriteInitial: false,
            initialPreviewAsData: true,
            showUpload: false,
            allowedFileExtensions: ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'htm', 'txt', 'rar', 'zip', 'gz', 'bz2', 'mp4', 'avi', 'rmvb', 'pdf']//接收的文件后缀
        });
        
        $("#fileinput").on("fileuploaded", function(event, data, proviewId, index) {
            console.info(event);
            console.info(data);
            console.info(proviewId);
            console.info(index);
        });
        
    });
    </script>
</body>
</html>